//
//  NetworkManager.m
//  jinwan
//
//  Created by 王尧 on 2017/5/26.
//  Copyright © 2017年 wy. All rights reserved.
//

#import "NetworkManager.h"
#import "NetworkBaseUrl.h"
NSString *const kNetResponse_M0008_3 = @"M0008_3";
NSString *const kNetResponse_R0001 = @"R0001";
NSString *const kNetResponse_R0003 = @"R0003";
NSString *const kNetResponse_E0001 = @"E0001";

@interface NetworkManager ()

@property (nonatomic, strong) AFHTTPSessionManager *manager;

@end

@implementation NetworkManager

- (id)init {
    if (self = [super init]) {
        self.manager = [AFHTTPSessionManager manager];
        self.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",
                          @"application/json",
                          @"text/plain",
                          @"text/json",
                          @"image/jpeg",
                          @"text/javascript", nil]; 
    }
    return self;
}

+ (instancetype)defaultManager {
    static NetworkManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[NetworkManager alloc] init];
    });
    return instance;
}

#pragma mark -- GET请求 --
- (void)getWithURLString:(NSString *)URLString
              parameters:(id)parameters
                 success:(void (^)(id))success
                 failure:(void (^)(NSError *))failure {
    /** 注意:断言只在Debug才会执行,Release不会执行 */
    NSAssert(URLString.length, @"URLString is null");
    [self.manager GET:URLString
           parameters:parameters
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  if (responseObject) {
                      id dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
                      success(dic);
                  }
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  if (failure) {
                      failure(error);
                  }
              }];
}

#pragma mark -- POST请求 --
- (void)postWithURLString:(NSString *)URLString
               parameters:(id)parameters
                  success:(void (^)(id))success
                  failure:(void (^)(NSError *))failure {
    /** 注意:断言只在Debug才会执行,Release不会执行 */
    NSAssert(URLString.length, @"URLString is null");
    [self.manager POST:URLString
            parameters:parameters
              progress:nil
               success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                   if (responseObject) {
                       id dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
                       success(dic);
                   }
               }
               failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                   if (failure) {
                       failure(error);
                   }
               }];
}

#pragma mark - 登录检测

- (void)check_getWithURLString:(NSString *)URLString
                    parameters:(id)parameters
                       success:(void (^)(id responseObject))success
                       failure:(void (^)(NSError *error))failure
{
    /** 注意:断言只在Debug才会执行,Release不会执行 */
    NSAssert(URLString.length, @"URLString is null");
    [self.manager GET:URLString
           parameters:parameters
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  if (responseObject) {
                      id dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
                      NSString *rcd = dic[@"rcd"];
                      if ([rcd isEqualToString:kNetResponse_E0001]) {
                          //非登录状态 并且请求要求验证
                         
                          
                          
                          
                          
                          
                      }
                      success(dic);
                  }
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  if (failure) {
                      failure(error);
                  }
              }];
}

- (void)check_postWithURLString:(NSString *)URLString
                     parameters:(id)parameters
                        success:(void (^)(id responseObject))success
                        failure:(void (^)(NSError *error))failure
{
    /** 注意:断言只在Debug才会执行,Release不会执行 */
    NSAssert(URLString.length, @"URLString is null");
    [self.manager POST:URLString
            parameters:parameters
              progress:nil
               success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                   if (responseObject) {
                       id dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
                       NSString *rcd = dic[@"rcd"];
                       if ([rcd isEqualToString:kNetResponse_E0001]) {
                           //非登录状态 并且请求要求验证
                           
                           
                           
                           
                           
                           
                       }
                       success(dic);
                   }
               }
               failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                   if (failure) {
                       failure(error);
                   }
               }];
}

- (NSURLSessionDataTask *)task_postWithURLString:(NSString *)URLString
                                      parameters:(id)parameters
                                         success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                                         failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    /** 注意:断言只在Debug才会执行,Release不会执行 */
    NSAssert(URLString.length, @"URLString is null");
    NSURLSessionDataTask * task = [self.manager POST:URLString
                                          parameters:parameters
                                            progress:nil
                                             success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                                                 
                                                 if (responseObject) {
                                                     id dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
                                                     NSString *rcd = dic[@"rcd"];
                                                     if ([rcd isEqualToString:kNetResponse_E0001]) {
                                                         //非登录状态 并且请求要求验证
                                                      
                                                         
                                                         
                                                         
                                                         
                                                     }
                                                     success(task, dic);
                                                 } else {
                                                     success(task, responseObject);
                                                 }
        
                                             }
                                             failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                                 if (failure) {
                                                     failure(task, error);
                                                 }
                                             }];
    return task;
}

#pragma mark - 设置Header
- (instancetype)addHeader:(NSString *)headerString
                 forField:(NSString *)fieldString {
    [self.manager.requestSerializer setValue:headerString
                          forHTTPHeaderField:fieldString];
    return self;
}


@end
